package com.imooc.bilibili.service;

import com.imooc.bilibili.dao.UserFollowIngDao;
import com.imooc.bilibili.domain.User;
import com.imooc.bilibili.domain.UserFollowing;
import com.imooc.bilibili.domain.UserFollowingGroup;
import com.imooc.bilibili.domain.UserInfo;
import com.imooc.bilibili.domain.constant.UserConstant;
import com.imooc.bilibili.domain.exception.ConditionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class UserFollowingService {

    @Autowired
    private UserFollowIngDao userFollowIngDao;

    @Autowired
    private UserFollowingGroupService userFollowingGroupService;

    @Autowired
    private UserService userService;

    @Transactional
    public void addUserFollowings(UserFollowing userFollowing) {

        //判断groupid
        Long groupId = userFollowing.getGroupId();
        if (groupId == null) {
            UserFollowingGroup userFollowingGroup = userFollowingGroupService.getByType(UserConstant.USER_FOLLOWING_GROUP_TYPE_DEFAULT);
            userFollowing.setGroupId(userFollowingGroup.getId());
        } else {
            UserFollowingGroup userFollowingGroup = userFollowingGroupService.getById(userFollowing.getGroupId());
            if (userFollowingGroup == null) {
                throw new ConditionException("关注分组不存在");
            }
        }
        //判断用户id存在
        Long followingId = userFollowing.getFollowingId();
        User user = userService.getUserById(followingId);

        if (user == null) {
            throw new ConditionException("关注永不存在");
        }
        //删除之前的旧数据
        userFollowIngDao.delete(userFollowing.getUserId(), followingId);
        //添加新数据
        userFollowing.setCreateTime(new Date());
        userFollowIngDao.addFollowing(userFollowing);
    }

    //获取关注的用户列表
    // 根据关注用户的id查询关注用户的基本信息
    // 将关注用户按关注分组进行分类
    public List<UserFollowingGroup> getUserFollowings(Long userId) {

        List<UserFollowing> userFollowingList = userFollowIngDao.getUserFollowingListById(userId);
        Set<Long> followingIds = userFollowingList.stream().map(UserFollowing::getFollowingId).collect(Collectors.toSet());

        List<UserInfo> userInfoList = new ArrayList<>();

        if (followingIds.size() > 0) {
            userInfoList = userService.getUserInfoByIds(followingIds);
        }
        List<UserFollowingGroup> userFollowingGroups = userFollowingGroupService.getGroupByUserId(userId);

        for (UserFollowing userFollowing : userFollowingList) {
            for (UserInfo userInfo : userInfoList) {
                if (userFollowing.getFollowingId().equals(userInfo.getUserId())) {
                    userFollowing.setUserInfo(userInfo);
                }
            }
        }
        ArrayList<UserFollowingGroup> result = new ArrayList<>();
        UserFollowingGroup allGroup = new UserFollowingGroup();

        allGroup.setName(UserConstant.USER_FOLLOWING_GROUP_ALL_NAME);
        allGroup.setUserInfoList(userInfoList);
        result.add(allGroup);

        for (UserFollowingGroup userFollowingGroup : userFollowingGroups) {

            List<UserInfo> userInfos = new ArrayList<>();

            for (UserFollowing userFollowing : userFollowingList) {

                if (userFollowingGroup.getId().equals(userFollowing.getGroupId())) {
                    userInfos.add(userFollowing.getUserInfo());
                }
            }

            userFollowingGroup.setUserInfoList(userInfos);
            result.add(userFollowingGroup);
        }
        return result;
    }

    // 获取当前粉丝列表
    // 根据粉丝的用户id查询基本信息
    // 查询当前用户是否已经关注改粉丝
    public List<UserFollowing> getUserFans(Long userId) {

        List<UserFollowing> fans = userFollowIngDao.getUserFans(userId);

        Set<Long> fansIds = fans.stream().map(UserFollowing::getUserId).collect(Collectors.toSet());

        List<UserInfo> fansInfos = new ArrayList<>();
        if (fansIds.size() > 0) {
            fansInfos = userService.getUserInfoByIds(fansIds);
        }

        List<UserFollowing> userFollowingList = userFollowIngDao.getUserFollowingListById(userId);

        for (UserFollowing fan : fans) {
            for (UserInfo fansInfo : fansInfos) {
                if (fan.getUserId().equals(fansInfo.getUserId())) {
                    fansInfo.setFollowed(false);
                    fan.setUserInfo(fansInfo);
                }
            }
        }
        for (UserFollowing userFollowing : userFollowingList) {
            for (UserInfo fansInfo : fansInfos) {
                if (userFollowing.getFollowingId().equals(fansInfo.getUserId())) {
                    fansInfo.setFollowed(true);
                    userFollowing.setUserInfo(fansInfo);
                }
            }
        }
        return fans;
    }

    public Long addUserFollowGroups(UserFollowingGroup userFollowingGroup) {

        userFollowingGroup.setCreateTime(new Date());
        userFollowingGroup.setType(UserConstant.USER_FOLLOWING_GROUP_TYPE_USER);
        userFollowingGroupService.addUserFollowGroups(userFollowingGroup);
        return userFollowingGroup.getId();
    }

    public List<UserFollowingGroup> getUserFollowingGroups(Long userId) {

        return userFollowingGroupService.getUserFollowingGroups(userId);
    }

    public List<UserInfo> checkFollowingStatus(List<UserInfo> userInfoList, Long userId) {

        List<UserFollowing> userFollowings = userFollowIngDao.getUserFollowingListById(userId);

        for (UserInfo userInfo : userInfoList) {
            userInfo.setFollowed(false);
            for (UserFollowing userFollowing : userFollowings) {
                if (userFollowing.getUserId().equals(userInfo.getUserId())) {
                    userInfo.setFollowed(true);
                }
            }
        }
        return userInfoList;
    }
}
